/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.my_web;

import com.mycompany.my_figure.SpecialFrame;
import com.mycompany.my_figure.Userdao;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static jdk.nashorn.internal.objects.NativeArray.map;

/**
 *
 * @author JSJSYS
 */
@WebServlet( urlPatterns = {"/Judge"})
public class Judge extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException {
            response.setContentType("text/html;charset=UTF-8");
            Userdao test = new Userdao();
            ResultSet rs;
            test.initDB();
            String ary[]=new String[2];//ary[0]为登陆id，ary[1]为密码
            ary[0]="";
            ary[1]="";
            String tmpn=request.getParameter("userName");
            String tmpp=request.getParameter("password");
            test.comparevalues(ary,tmpn);
            rs=test.selectalluser();
            Map<String,List<String>>map = new HashMap<String,List<String>>();
            while(rs.next())
            {
                List<String>listtmp=new ArrayList<String>();
                listtmp.add(rs.getString(3));
                listtmp.add(rs.getString(4));
                map.put(rs.getString(1),listtmp);
            }
            rs.close();
            request.setAttribute("list",map );
            if(ary[0].equals(tmpn)&&ary[1].equals(tmpp))
            {
                request.getRequestDispatcher("jsp/ok.jsp").forward(request, response);
            }
            else if(ary[1]==""&&ary[0]=="") 
            {
                request.setAttribute("msg", "用户名不存在");
                request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
            }
            else
            {
                request.setAttribute("userName", ary[0]);
                request.setAttribute("msg", "密码错误");
                request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
            }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(Judge.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(Judge.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
